﻿using Hxj.Data;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Json;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RecruitStudents.Client.BLL.Impl
{
    public class BLLFinancial
    {
        DAL.DALFinancial dal = new DAL.DALFinancial();
        #region 获取考试缴费信息

        public DataTable GetExamineePayFee(string strExamPlaceNO, string strIdentityCard)
        {
            return dal.GetExamineePayFee(strExamPlaceNO, strIdentityCard);
        }
        #endregion

        #region  保存缴费确认的信息（更新缴费状态字段 保存财务订单付款 保存财务订单明细）
        public JsonMessage UpdateSpecialityInfoState(string strJsonPayFeeModel, string strJsonPayFee_DetailList, string strJsonStudentsSpeciality)
        {
            TB_SignUpStudentsNetPayFee_Head PayFeeModel = strJsonPayFeeModel.ToList<TB_SignUpStudentsNetPayFee_Head>();
            List<TB_SignUpStudentsNetPayFee_Detail> PayFee_Detaillist = strJsonPayFee_DetailList.ToList<List<TB_SignUpStudentsNetPayFee_Detail>>();
            List<TB_Exam_SignUpStudentsSpeciality> StudentsSpecialitylist = strJsonStudentsSpeciality.ToList<List<TB_Exam_SignUpStudentsSpeciality>>();

            JsonMessage message = new JsonMessage();
            DbTrans trans = DbHelper.GetDatabase().BeginTransaction();
            try
            {
                dal.SaveSignUpNetPayFee(PayFeeModel, trans);
                dal.SaveSignUpNetPayFeeDetail(PayFee_Detaillist, trans);
                dal.UpdateSpecialityInfoState(StudentsSpecialitylist, trans);
                trans.Commit();
                message.State = "true";
                message.Msg = "保存成功!";
            }
            catch (Exception ex)
            {
                trans.Rollback();
                message.State = "false";
                message.Msg = "保存失败," + ex.Message;
            }
            finally
            {
                trans.Close();
            }
            return message;
        }
        #endregion

        #region 查询 考试缴费信息
        public DataTable SearchPayFeeInfo(string strExamPlaceID, string strExamLevel, string strSpecialityKindsNO, string strPaySource,
            string IsFee, string StartDate, string EndDate, string OrdNO)
        {
            WhereClip clip = new WhereClip();
            //clip = clip.And(TB_SignUpStudentsNetPayFee_Head._.IsFee == "1");
            //clip = clip.And(TB_SignUpStudentsNetPayFee_Head._.IsFeeSuccess == "1");
            if (!string.IsNullOrEmpty(strExamPlaceID))
            {
                clip = clip.And(TB_SignUpStudentsNetPayFee_Detail._.ExamPlaceNO == strExamPlaceID);
            }
            if (!string.IsNullOrEmpty(strExamLevel))
            {
                clip = clip.And(TB_SignUpStudentsNetPayFee_Detail._.ExamLevel == strExamLevel);
            }
            if (!string.IsNullOrEmpty(strSpecialityKindsNO))
            {
                clip = clip.And(TB_SignUpStudentsNetPayFee_Detail._.SpecialityKindsNO == strSpecialityKindsNO);
            }
            if (!string.IsNullOrEmpty(strPaySource))
            {
                clip = clip.And(TB_SignUpStudentsNetPayFee_Head._.PaySource == strPaySource);
            }
            if (!string.IsNullOrEmpty(StartDate))
            {
                clip = clip.And(TB_SignUpStudentsNetPayFee_Head._.ChargDate >= StartDate.ToSafeDateTime());
            }
            if (!string.IsNullOrEmpty(EndDate))
            {
                DateTime dateTime = EndDate.ToSafeDateTime().AddDays(1);
                clip = clip.And(TB_SignUpStudentsNetPayFee_Head._.ChargDate < dateTime);
            }
            if (!string.IsNullOrEmpty(OrdNO))
            {
                clip = clip.And(TB_SignUpStudentsNetPayFee_Detail._.OrdNO == OrdNO);
            }
            if (!string.IsNullOrEmpty(IsFee))
            {
                clip = clip.And(TB_SignUpStudentsNetPayFee_Head._.IsFee.IsNull("0") == IsFee);
                //clip = clip.And(TB_SignUpStudentsNetPayFee_Detail._.OrdNO.SubQueryIn(
                //      DbHelper.GetDatabase().From<TB_SignUpStudentsNetPayFee_Head>()
                //     .Select(TB_SignUpStudentsNetPayFee_Head._.OrdNO)
                //     .Where(TB_SignUpStudentsNetPayFee_Head._.IsFee.IsNull("0") == IsFee)
                //     ));
            }
            return dal.SearchPayFeeInfo(clip);
        }
        #endregion

        #region 获取 考生报考专业,考试级别(根据考生ID)
        public DataTable GetExameeSpeciality_ExamLevelBySignUpID(string strIdCard)
        {
            WhereClip clip = new WhereClip();
            TB_SignUpStudents model = DbHelper.GetDatabase().From<TB_SignUpStudents>().Where(TB_SignUpStudents._.IdentityCard == strIdCard).ToFirst();
            if (model != null)
            {
                clip = clip.And(TB_Exam_SignUpStudentsSpeciality._.SignUpID == model.SignUpID);
            }
            else
            {
                clip = clip.And(TB_Exam_SignUpStudentsSpeciality._.SignUpID == "");
            }
            clip = clip.And(TB_Exam_SignUpStudentsSpeciality._.IsActive == "1");
            return dal.GetExameeSpeciality_ExamLevelBySignUpID(clip);
        }
        #endregion
    }
}
